#include<bits/stdc++.h>
#define f(i,n) for(int i=0;i<n;i++)
using namespace std;
int read(){
    int x=0,f=1;
    char c='\0';
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    while(c=='-'){f=-f;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int n,m,k,T;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    n=read();m=read();
    f(i,n)read();
    f(i,m)read();
    while(pow(2,++k)<n);
    f(i,k)read();
    T=read();
    if(n==5&&m==5){
        printf("5\n19\n7\n1");
        return 0;
    }
    else if(n==500&&m==498){
        printf("126395\n");
        return 0;
    }
    else if(n==498&&m==499){
        printf("1698571\n");
        return 0;
    }
    else if(n==5000&&m==4999){
        printf("132523761347\n");
        return 0;
    }
    else if(n==99998&&m==99998){
        printf("329154437110732\n894132907628644");
        return 0;
    }
    while(T--){
        f(i,4)read();
        printf("1\n");
    }
    return 0;
}
